/***********************************************************************
    filename:   hgeRendererManager.h
    created:    Jan 11 2006
    author:     Tomas Lindquist Olsen

    purpose:    Defines interface for the WindowRendererManager class
*************************************************************************/

#ifndef _hgeRendererManager_h_
#define _hgeRendererManager_h_

#include "hgeSingleton.h"
#include "hgeGuiWindowRenderer.h"
#include "hgeLogger.h"
#include "hgeExceptions.h"
#include "renderers/StaticRenderer.h"
#include <map>

#if defined(_MSC_VER)
#   pragma warning(push)
#   pragma warning(disable : 4251)
#endif

class WindowRendererManager : public Singleton<WindowRendererManager>
{
public:
    /*************************************************************************
        Contructor / Destructor
    *************************************************************************/
    WindowRendererManager();
    ~WindowRendererManager();

    /*************************************************************************
        Singleton
    *************************************************************************/
    static WindowRendererManager& getSingleton();
    static WindowRendererManager* getSingletonPtr();

	/*************************************************************************
	Accessors
	*************************************************************************/
	bool isFactoryPresent(const String& name) const;
	WindowRendererFactory* getFactory(const String& name) const;

	/*************************************************************************
	Manipulators
	*************************************************************************/
	template <typename T>
	static void addFactory();

	void addFactory(WindowRendererFactory* wr);
	void removeFactory(const String& name);

    /*************************************************************************
        Factory usage
    *************************************************************************/
    WindowRenderer* createWindowRenderer(const String& name);
    void destroyWindowRenderer(WindowRenderer* wr);

private:
	/*************************************************************************
	Implementation data
	*************************************************************************/
	typedef std::map<String, WindowRendererFactory*> WR_Registry;
	WR_Registry d_wrReg;

	//! Container type to hold WindowRenderFacory objects that we created.
	typedef std::vector<WindowRendererFactory*> OwnedFactoryList;
	//! Container that tracks WindowFactory objects we created ourselves.
	static OwnedFactoryList d_ownedFactories;

	void registerFactory();
};

//----------------------------------------------------------------------------//
template <typename T>
void WindowRendererManager::addFactory()
{
	// create the factory object
	WindowRendererFactory* factory = new T;

	// only do the actual add now if our singleton has already been created
	if (WindowRendererManager::getSingletonPtr())
	{
		hgeLogger::getSingleton().logEvent("Created WindowRendererFactory for '" +
			factory->getName() +
			"' WindowRenderers.");
		// add the factory we just created
		try
		{
			WindowRendererManager::getSingleton().addFactory(factory);
		}
		catch (Exception&)
		{
			hgeLogger::getSingleton().logEvent("Deleted WindowRendererFactory for "
				"'" + factory->getName() +
				"' WindowRenderers.");
			// delete the factory object
			delete factory;
			throw;
		}
	}

	d_ownedFactories.push_back(factory);
}

#if defined(_MSC_VER)
#   pragma warning(pop)
#endif

#endif // _hgeRendererManager_h_
